﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using StoreVnCore.Entity;
using StoreVnCore.Common;
using StoreVnCore.Interface;
using StoreVnCore.ExceptionManager;
using System.Data;

namespace StoreVnCore.DataAccessObject
{
    /// <summary>
    /// Class execute access database of table TypeOfContent
    /// </summary>
    public class TypeOfContentDAO : BaseDAO, ITypeOfContentDAO
    {
        #region Member
        protected const string STORE_INSERT = "StoreVn_Sp_TypeOfContent_Insert";
        protected const string STORE_UPDATE = "StoreVn_Sp_TypeOfContent_Update";
        protected const string STORE_DELETE = "StoreVn_Sp_TypeOfContent_Delete";
        protected const string STORE_SELECTONE = "StoreVn_Sp_TypeOfContent_SelectOneById";
        protected const string STORE_SELECTALL = "StoreVn_Sp_TypeOfContent_SelectAll";
        #endregion

        #region Method
        /// <summary>
        /// Execute insert data into database of table TypeOfContent
        /// </summary>
        public TypeOfContentEntity Insert(TypeOfContentEntity typeOfContent)
        {
            #region Parameter
            SqlParameter[] prm = new SqlParameter[3];
            prm[0] = new SqlParameter(FieldNames.TypeOfContent.Id, SqlDbType.Int, 4);
            prm[0].Direction = ParameterDirection.Output;
            prm[1] = new SqlParameter(FieldNames.TypeOfContent.Name, typeOfContent.Name);
            prm[2] = new SqlParameter(FieldNames.TypeOfContent.Description, typeOfContent.Description);
            #endregion
            #region Execute
            try
            {
                ExeNonQuery(STORE_INSERT, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("TypeOfContentDAO::Insert::Error", ex);
            }
            typeOfContent.Id = (int)prm[0].Value;
            #endregion
            return typeOfContent;
        }


        /// <summary>
        /// Execute update data into database of table TypeOfContent
        /// </summary>
        public TypeOfContentEntity Update(TypeOfContentEntity typeOfContent)
        {
            #region Parameter
            SqlParameter[] prm = new SqlParameter[3];
            prm[0] = new SqlParameter(FieldNames.TypeOfContent.Id, typeOfContent.Id);
            prm[1] = new SqlParameter(FieldNames.TypeOfContent.Name, typeOfContent.Name);
            prm[2] = new SqlParameter(FieldNames.TypeOfContent.Description, typeOfContent.Description);
            #endregion
            #region Execute
            try
            {
                ExeNonQuery(STORE_UPDATE, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("TypeOfContentDAO::Update::Error", ex);
            }
            #endregion
            return typeOfContent;
        }


        /// <summary>
        /// Execute delete data from database of table Category
        /// </summary>
        public void Delete(int Id)
        {
            #region Parameter
            SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter(FieldNames.TypeOfContent.Id, Id);
            #endregion
            #region Execute
            try
            {
                ExeNonQuery(STORE_DELETE, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("TypeOfContentDAO::Delete::Error", ex);
            }
            #endregion
        }


        /// <summary>
        /// Execute select one record from database of table Category
        /// </summary>
        public TypeOfContentEntity SelectOne(int Id)
        {
            TypeOfContentEntity typeOfContent = null;
            DataTable dt = null;
            #region Parameter
            SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter(FieldNames.TypeOfContent.Id, Id);
            #endregion
            #region Execute
            try
            {
                dt = ExeDataTable(STORE_SELECTONE, prm);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("TypeOfContentDAO::SelectOne::Error", ex);
            }
            #endregion
            #region BindData to Entity
            if (dt != null && dt.Rows.Count > 0)
            {
                typeOfContent = new TypeOfContentEntity();
                DataRow dr = dt.Rows[0];
                //Id
                if (dr[FieldNames.TypeOfContent.Id] != null && dr[FieldNames.TypeOfContent.Id].ToString() != "")
                {
                    typeOfContent.Id = (int)dr[FieldNames.TypeOfContent.Id];
                }

                //Name
                if (dr[FieldNames.TypeOfContent.Name] != null && dr[FieldNames.TypeOfContent.Name].ToString() != "")
                {
                    typeOfContent.Name = (string)dr[FieldNames.TypeOfContent.Name];
                }

                //Description
                if (dr[FieldNames.TypeOfContent.Description] != null && dr[FieldNames.TypeOfContent.Description].ToString() != "")
                {
                    typeOfContent.Description = (string)dr[FieldNames.TypeOfContent.Description];
                }
            }
            #endregion
            return typeOfContent;
        }


        /// <summary>
        /// Execute select all data from database of table Category
        /// </summary>
        public DataTable SelectAll()
        {
            DataTable dt = null;
            #region Execute
            try
            {
                dt = ExeDataTable(STORE_SELECTALL);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("TypeOfContentDAO::SelectAll::Error", ex);
            }
            #endregion
            return dt;
        }
        #endregion
    }
}
